package com.lsht.web.controller.siwei;

import com.github.pagehelper.PageInfo;
import com.lsht.common.bo.IdListAcceptBo;
import com.lsht.common.core.controller.BaseController;
import com.lsht.common.core.domain.AjaxResult;
import com.lsht.common.core.domain.model.LoginUser;
import com.lsht.common.utils.ServletUtils;
import com.lsht.framework.web.service.TokenService;
import com.lsht.system.domain.siwei.SiweiDevice;
import com.lsht.system.domain.siwei.SiweiImg;
import com.lsht.system.service.siwei.SiweiImgService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * 图片管理Controller
 *
 * @author zyc
 * @date 2024-08-14
 */
@RestController
@RequestMapping("/siwei/img")
@Api(value = "SiweiImg", description = "图片管理", tags = "图片管理")
public class SiweiImgController extends BaseController {
    @Autowired
    private SiweiImgService siweiImgService;

    @Autowired
    private TokenService tokenService;

    /**
     * 新增图片管理
     */
    @PostMapping(ADD_FUNCTION_URL)
    @ApiOperation(value = "新增上传图片(新增和修改用同一接口即可)", httpMethod = "POST", response = AjaxResult.class, tags = "图片管理")
    //@PreAuthorize("@ss.hasPermi('siwei:img:add')")
    public AjaxResult genAdd(SiweiImg siweiImg, MultipartFile file) {
        return toAjax(siweiImgService.genAdd(siweiImg, file));
    }

    /**
     * 新增图片管理app端使用
     */
    @PostMapping("addImgByApp")
    @ApiOperation(value = "新增图片管理app端使用(新增和修改用同一接口即可)", httpMethod = "POST", response = AjaxResult.class, tags = "图片管理")
    public AjaxResult addImgByApp(@RequestBody SiweiImg siweiImg) {
        return toAjax(siweiImgService.addImgByApp(siweiImg));
    }

    /**
     * 删除图片管理
     */
    @PostMapping(DELETE_BY_ID_FUNCTION_URL)
    @ApiOperation(value = DELETE_BY_ID_FUNCTION, httpMethod = "POST", response = AjaxResult.class, tags = "图片管理")
    //@PreAuthorize("@ss.hasPermi('siwei:img:remove')")
    public AjaxResult genDeleteById(@RequestBody IdListAcceptBo idListAcceptBo) {
        List<String> idList = idListAcceptBo.getIdList();
        if (CollectionUtils.isNotEmpty(idList)) {
            return toAjax(siweiImgService.genDeleteById(idList));
        }
        return AjaxResult.error();
    }

    /**
     * 修改图片管理
     */
    @PostMapping(UPDATE_BY_ID_FUNCTION_URL)
    @ApiOperation(value = UPDATE_BY_ID_FUNCTION, httpMethod = "POST", response = AjaxResult.class, tags = "图片管理")
    //@PreAuthorize("@ss.hasPermi('siwei:img:edit')")
    public AjaxResult genUpdateById(@RequestBody SiweiImg siweiImg) {
        return toAjax(siweiImgService.genUpdateById(siweiImg));
    }

    /**
     * 查询图片管理列表
     */
    @PostMapping(QUERY_LIST_FUNCTION_URL)
    @ApiOperation(value = QUERY_LIST_FUNCTION, httpMethod = "POST", response = SiweiImg.class, tags = "图片管理")
    //@PreAuthorize("@ss.hasPermi('siwei:img:list')")
    public AjaxResult genQueryList(@RequestBody SiweiImg siweiImg) {
        startPage();
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        boolean admin = loginUser.getUser().isAdmin();
        Long deptId = null;
        if (admin) {
            deptId = null;
        } else {
            //获取部门ID
            deptId = loginUser.getUser().getDeptId();
        }
        List<SiweiImg> list = siweiImgService.genQueryList(siweiImg, deptId);
        return AjaxResult.success(new PageInfo<>(list));
    }

    /**
     * 图片管理不分页接口新增点位下拉使用
     */
    @PostMapping("nogPageImageList")
    @ApiOperation(value = "图片管理不分页接口新增点位下拉使用", httpMethod = "POST", response = SiweiImg.class, tags = "图片管理")
    public AjaxResult nogPageImageList(@RequestBody SiweiImg siweiImg) {
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        boolean admin = loginUser.getUser().isAdmin();
        Long deptId = null;
        if (admin) {
            deptId = null;
        } else {
            //获取部门ID
            deptId = loginUser.getUser().getDeptId();
        }
        List<SiweiImg> list = siweiImgService.genQueryList(siweiImg, deptId);
        return AjaxResult.success(list);
    }


    /**
     * 查询图片管理列表不分页
     */
    @PostMapping("notPageGenQueryList")
    @ApiOperation(value = "查询图片管理列表不分页", httpMethod = "POST", response = SiweiImg.class, tags = "图片管理")
    public AjaxResult notPageGenQueryList(@RequestBody SiweiImg siweiImg) {
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        boolean admin = loginUser.getUser().isAdmin();
        Long deptId = null;
        if (admin) {
            deptId = null;
        } else {
            //获取部门ID
            deptId = loginUser.getUser().getDeptId();
        }
        List<SiweiImg> list = siweiImgService.notPageGenQueryList(siweiImg, deptId);
        return AjaxResult.success(list);
    }

    /**
     * 根据id查询图片管理详情信息
     */
    @PostMapping(QUERY_BY_ID_FUNCTION_URL)
    @ApiOperation(value = QUERY_BY_ID_FUNCTION, httpMethod = "POST", response = SiweiDevice.class, tags = "图片管理")
    //@PreAuthorize("@ss.hasPermi('siwei:img:query')")
    public AjaxResult genQueryById(@RequestBody SiweiImg siweiImg) {
        SiweiImg img = siweiImgService.genQueryById(siweiImg);
        return AjaxResult.success(img);
    }
}
